package com.onetwork.core.actions;

import com.onetwork.core.ui.components.BasicWindow;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JTabbedPane;

public class AbrirCadastroAction extends AbstractAction {
    private static final long serialVersionUID = 1L;
    private final JTabbedPane tabbedPane;
    private final BasicWindow basicPanel;
    
    public AbrirCadastroAction(String title, JTabbedPane tabbedPane, BasicWindow basicPanel) {
        super(title, basicPanel.icon());
        this.basicPanel = basicPanel;
        this.tabbedPane = tabbedPane;
    }
    
    public BasicWindow basicWindow() {
        return this.basicPanel;
    }
    
    @Override
    public void actionPerformed(ActionEvent event) {
        int index = this.tabbedPane.indexOfComponent(this.basicPanel);
        if (index == -1) {
            this.tabbedPane.addTab(this.basicPanel.getName(), this.basicPanel.icon(), this.basicPanel);
            int pos = this.tabbedPane.indexOfComponent(this.basicPanel);
            this.tabbedPane.setTabComponentAt(pos, new ButtonTabComponent(tabbedPane));
            this.tabbedPane.setIconAt(pos, this.basicPanel.icon());
        }
        this.tabbedPane.setSelectedComponent(this.basicPanel);
    }
}
